/**
 * Copyright (c) 2005 - 2007 FeyaSoft Corp. All Rights Reserved.
 */
package com.feyaSoft.home.services.demouser;

import java.util.Date;
import java.util.Set;

import org.apache.log4j.Logger;

import com.feyaSoft.home.dao.base.BaseSearchResult;
import com.feyaSoft.home.dao.demouser.DemouserDao;
import com.feyaSoft.home.dao.demouser.DemouserSearchCriteria;
import com.feyaSoft.home.hibernate.demouser.Demouser;
import com.feyaSoft.home.services.base.SearchBaseInfo;
import com.feyaSoft.home.services.base.Service;
import com.feyaSoft.plugin.util.CSVParser;
import com.feyaSoft.plugin.util.ObjectCheck;

/**
 * @author Fenqiang Zhuang
 * @Apr 29, 2007
 * 
 * This file is used to .....
 */
public class DemouserServiceImpl implements Service, DemouserService {
	// log file
	private final static Logger log = Logger
			.getLogger(DemouserServiceImpl.class);
	private DemouserDao demouserDAO;

	/**
	 * get a list of demo user now
	 */
	public BaseSearchResult getList(SearchBaseInfo searchBaseInfo)
			throws Exception {

		ListDemouserInfo listDemouserInfo = (ListDemouserInfo) searchBaseInfo;
		// set search criteria now
		DemouserSearchCriteria searchCriteria = new DemouserSearchCriteria();

		// set find field/value
		String field = searchBaseInfo.getField().trim();
		String findValue = searchBaseInfo.getFieldValue().trim();

		// see the field value
		if (field.equalsIgnoreCase("username")
				&& (!ObjectCheck.isEmptyString(findValue))) {
			CSVParser stringParser = new CSVParser(findValue, true);
			Set<String> stringInSet = stringParser.getAllFieldsInSet();
			searchCriteria.setUsernames(stringInSet);
		}

		// see the field value
		if (field.equalsIgnoreCase("name")
				&& (!ObjectCheck.isEmptyString(findValue))) {
			CSVParser stringParser = new CSVParser(findValue, true);
			Set<String> stringInSet = stringParser.getAllFieldsInSet();
			searchCriteria.setNames(stringInSet);
		}

		// set default search order
		searchCriteria.setOrderProperty("lastName");

		int rowsPerPage = searchBaseInfo.getRowsPerPage();
		int firstResultPos = (searchBaseInfo.getCurrentPage() - 1)
				* rowsPerPage;
		searchCriteria.setFirstResult(new Long(firstResultPos));
		searchCriteria.setTotalResults(new Integer(rowsPerPage));
		searchCriteria.setNeedCount(Boolean.TRUE);

		return demouserDAO.getDemouserList(searchCriteria);
	}

	/**
	 * create/update new demouser
	 */
	public boolean saveDemouser(Demouser demouser) throws Exception {
		boolean saveOk = false;

		try {
			if (demouser.getId() == null) {
				demouser.setCreationDate(new Date());
			}
			demouser.setUpdateDate(new Date());
			demouserDAO.saveOrUpdate(demouser);
			saveOk = true;
		} catch (Exception e) {
			// do nothing ...
			e.printStackTrace();
			log.error("saveDemousero failed, please investigate");
		}

		return saveOk;
	}

	/**
	 * load timesheet now by id...
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public Demouser getDemouserById(Long id) throws Exception {
		Demouser demouser = null;
		try {
			demouser = demouserDAO.loadDemouserById(id);
		} catch (Exception e) {
			log.error("load Demouser by id failed: id = " + id);
			e.printStackTrace();
		}

		return demouser;
	}

	/**
	 * Load Demouser by username
	 */
	public Demouser loadDemouserByUsername(String username) {
		Demouser demouser = null;
		try {
			demouser = demouserDAO.loadDemouserByUsername(username);
		} catch (Exception e) {
			e.printStackTrace();
			log.error("Exception during call loadDemouserByUsername");
		}
		return demouser;
	}

	/**
	 * Delete user now...
	 * 
	 * @param id
	 * @return
	 */
	public boolean deleteDemouser(Long id) {
		boolean result = false;
		try {
			demouserDAO.delete(getDemouserById(id));
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
			log.error("Exception for method - deleteDemouser");
		}
		return result;
	}

	public void setDemouserDAO(DemouserDao demouserDAO) {
		this.demouserDAO = demouserDAO;
	}

}
